package com.pctx.devicemanager.service.impl;

import com.pctx.common.util.UuidUtil;
import com.pctx.devicemanager.dao.DeviceUserMapper;
import com.pctx.devicemanager.pojo.DeviceUser;
import com.pctx.devicemanager.pojo.DeviceUserExample;
import com.pctx.devicemanager.service.DeviceUserService;
import com.pctx.devicemanager.service.UserDeviceRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by chason on 2018/3/1.10:43
 */
@Service("deviceUserService")
public class DeviceUserServiceImpl implements DeviceUserService {
    @Autowired
    private DeviceUserMapper deviceUserMapper;
    @Autowired
    private UserDeviceRoleService userDeviceRoleService;

    @Override
    public boolean add(DeviceUser du) throws Exception {
        if (StringUtils.isEmpty(du.getUserUuid()))
            return false;

        // 判断是否已经关联
        DeviceUser old = getByDeviceUuidAndUserUuid(du.getDeviceUuid(), du.getUserUuid());
        if (null != old)
            return false;

        du.setUuid(UuidUtil.randomUUID());

        boolean flag = deviceUserMapper.insertSelective(du) > 0;
        if (!flag)
            return flag;

        // 添加设备用户-角色关联关系
        if (StringUtils.isNotEmpty(du.getRoleUuids())) {
            userDeviceRoleService.add(du.getUuid(), du.getRoleUuids());
        }

        return flag;
    }

    @Override
    public boolean update(DeviceUser du) throws Exception {
        if (StringUtils.isEmpty(du.getUuid()))
            return false;

        DeviceUser old = getByUuid(du.getUuid());
        if (null == old)
            return false;

        du.setId(old.getId());
        boolean flag = deviceUserMapper.updateByPrimaryKeySelective(du) > 0;

        if (!flag)
            return flag;

        // 删除旧的设备用户-角色关联关系
        userDeviceRoleService.delByUserDeviceUuid(du.getUuid());

        // 添加新的设备用户-角色关联关系
        if (StringUtils.isNotEmpty(du.getRoleUuids())) {
            userDeviceRoleService.add(du.getUuid(), du.getRoleUuids());
        }

        return flag;
    }

    @Override
    public DeviceUser getByUuid(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return null;

        DeviceUserExample example = new DeviceUserExample();
        DeviceUserExample.Criteria criteria = example.createCriteria();
        criteria.andUuidEqualTo(uuid);

        List<DeviceUser> list = deviceUserMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        return list.get(0);
    }

    @Override
    public List<DeviceUser> getByDeviceUuid(String deviceUuid) throws Exception {
        if (StringUtils.isEmpty(deviceUuid))
            return null;

        DeviceUserExample example = new DeviceUserExample();
        DeviceUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceUuidEqualTo(deviceUuid);

        List<DeviceUser> list = deviceUserMapper.selectByExample(example);

        return list;
    }

    @Override
    public DeviceUser getByDeviceUuidAndUserUuid(String deviceUuid, String userUuid) throws Exception {
        if (StringUtils.isEmpty(deviceUuid) || StringUtils.isEmpty(userUuid))
            return null;

        DeviceUserExample example = new DeviceUserExample();
        DeviceUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceUuidEqualTo(deviceUuid).andUserUuidEqualTo(userUuid);

        List<DeviceUser> list = deviceUserMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        return list.get(0);
    }
}
